package com.slz3.business.module.memberManagement.memPoint.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.memPoint.domain.MemPoint;
import com.slz3.business.module.memberManagement.memPoint.domain.MemPointFactory;
import com.slz3.business.module.memberManagement.memPoint.steward.MemPointSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MemPointServiceImpl implements MemPointService {
    @Override
    public MemPoint save(MemPoint memPoint) {
        return Repositorys.memPoint.save(MemPointFactory.initBaseFields(memPoint));
    }
    @Override
    public Page<MemPoint> page(MultiValueMap<String, String> params) {
        Page<MemPoint> memPointPage = Repositorys.memPoint.findAll(
                MemPointSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memPointPage;
    }


    @Override
    public MemPoint modify(MemPoint memPointNew) {
        return Repositorys.memPoint.saveAndFlush(MemPointFactory.initBaseFields(memPointNew));
    }

    @Override
    public Optional<MemPoint> single(UUID uuid) {
        return Repositorys.memPoint.findById(uuid);
    }

    @Override
    public List<MemPoint> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memPoint.findAllById(uuids);
    }

    @Override
    public List<MemPoint> delete(List<MemPoint> memPointList) {
        memPointList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemPointFactory.initBaseFields(item);
        });
        return Services.memPointTransactional.batchModify(memPointList);
    }

}
